package com.kmust.dream.wanderingdream.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.comm.pojo.ReturnMessage;
import com.kmust.dream.wanderingdream.product.entity.ProductProperty;
import com.kmust.dream.wanderingdream.product.entity.ProductPropertyName;
import com.kmust.dream.wanderingdream.product.service.ProductPropertyNameService;
import com.kmust.dream.wanderingdream.product.mapper.ProductPropertyNameMapper;
import com.kmust.dream.wanderingdream.product.service.ProductPropertyService;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class ProductPropertyNameServiceImpl extends ServiceImpl<ProductPropertyNameMapper, ProductPropertyName>
        implements ProductPropertyNameService {

    @Autowired
    ProductPropertyService productPropertyService;

    @Override
    public ResultBean getPropertyNameByPropertyNameId(Integer productPropertyNameId) {
        return ReturnUtils.getOne(this.getById(productPropertyNameId));
    }

    @Override
    public ResultBean addProductPropertyName(ProductPropertyName productPropertyName) {
        return ReturnUtils.add(this.save(productPropertyName));
    }

    @Override
    public ResultBean updateProductPropertyName(ProductPropertyName productPropertyName) {
        return ReturnUtils.update(this.updateById(productPropertyName));
    }

    @Override
    public ResultBean deleteProductPropertyName(Integer productPropertyNameId) {
        return ReturnUtils.delete(this.removeById(productPropertyNameId));
    }

    @Override
    public ResultBean getAllPropertyNames() {
        return ReturnUtils.getList(this.list());
    }

    @Override
    public ResultBean getPropertyNameByCategoryId(Integer categoryId) {
        return ReturnUtils.getList(this.list(new QueryWrapper<ProductPropertyName>().eq("product_category_id", categoryId)));
    }

    @Override
    public ResultBean getPropertyNames(Integer productId) {
        ResultBean propertyName = productPropertyService.getPropertyName(productId);
        if (propertyName.getStatus().equals(ReturnMessage.GET_SUCCESS.getCode())) {
            Object payload = propertyName.getPayload();
            List<ProductProperty> payload1 = (List<ProductProperty>) payload;
            List<Integer> nameIds = new ArrayList<>();
            for (ProductProperty productProperty : payload1) {
                nameIds.add(productProperty.getProductPropertyNameId());
            }
            QueryWrapper<ProductPropertyName> in = new QueryWrapper<ProductPropertyName>().in("product_property_name_id", nameIds);
            return ReturnUtils.getList(list(in));
        } else {
            return propertyName;
        }
    }

    @Override
    public ResultBean getPropertyNameByProductId(Integer productCategoryId) {
        return ReturnUtils.getList(list(new QueryWrapper<ProductPropertyName>().eq("product_category_id", productCategoryId)));
    }
}




